package com.budwk.app.waterquality.services.impl;

import com.budwk.app.sys.models.Sys_unit;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.waterquality.component.CommonComponent;
import com.budwk.app.waterquality.vo.QueryCommonParamVo;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.app.waterquality.models.WaterSectionInfo;
import com.budwk.app.waterquality.services.WaterSectionInfoService;
import org.apache.commons.compress.harmony.unpack200.bytecode.forms.ThisInitMethodRefForm;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import java.time.LocalDateTime;

@IocBean(args = {"refer:dao"})
public class WaterSectionInfoServiceImpl extends BaseServiceImpl<WaterSectionInfo> implements WaterSectionInfoService {
    public WaterSectionInfoServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public Result<?> listByPage(QueryCommonParamVo reqVo) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(reqVo.getPageOrderBy())) {
            cnd.orderBy(reqVo.getPageOrderName(), PageUtil.getOrder(reqVo.getPageOrderBy()));
        }
        Pagination pagination = this.listPage(reqVo.getPageNo(), reqVo.getPageSize(), cnd);
        return Result.data(pagination);
    }

    @Override
    public void create(WaterSectionInfo water) {
        Sys_user user = CommonComponent.getCurrentUser();
        Sys_unit unit = user.getUnit();
        water.setCreateId(user.getId()).setCreateDate(LocalDateTime.now());
        water.setUpdateId(user.getId()).setUpdateDate(LocalDateTime.now());
        water.setUploadName(user.getUsername()).setUploadDate(LocalDateTime.now());
        water.setUnitId(unit != null ? unit.getId() : null).setUnitName(unit != null ? unit.getName() : null);
        this.insert(water);
    }
}
